﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FreedomX2.Core;

namespace FreedomX2.Core.IO
{
    public static class StringCtrl
    {
        public static string SplitBy(this string strVal, char pice, int postion, ABSet sets)
        {
            try
            {
                string a = "", b = "";
                if (postion > 0)
                {
                    int lastpostion = 0;
                    for (int i = 0; i < postion; i++)
                    {
                        lastpostion = strVal.IndexOf(pice, lastpostion);
                        a = strVal.Substring(0, lastpostion);
                        lastpostion += 1;
                    }
                    b = strVal.Substring(lastpostion);

                    if (sets == ABSet.Before)
                    {
                        return a;
                    }
                    else
                    {
                        return b;
                    }
                }
                if (postion == 0)
                {
                    int lastpostion = 0;
                    lastpostion = strVal.IndexOf(pice, lastpostion);
                    a = strVal.Substring(0, lastpostion);
                    lastpostion += 1;
                    b = strVal.Substring(lastpostion);

                    if (sets == ABSet.Before)
                    {
                        return a;
                    }
                    else
                    {
                        return b;
                    }
                }
                if (postion < 0)
                {
                    int lastpostion = strVal.Length - 1;
                    for (int i = 0; i < Math.Abs(postion); i++)
                    {
                        lastpostion = strVal.LastIndexOf(pice, lastpostion);
                        a = strVal.Substring(lastpostion + 1);
                        lastpostion -= 1;
                    }
                    b = strVal.Substring(0, lastpostion + 1);

                    if (sets == ABSet.Before)
                    {
                        return b;
                    }
                    else
                    {
                        return a;
                    }
                }

            }
            catch { return strVal; }

            return null;
        }
    }
}
